package dao;

import pojo.Doctor;
import utils.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 陈金平
 * @Description
 * @create 2020/11/18 14:47
 */
public class DoctorDaoImpl implements DoctorDao{
    @Override
    public List<Doctor> selectAllDoctors(Map<String, Object> params) {
        List<Doctor> doctors = new ArrayList<>();
        Integer start = (Integer) params.get("start");
        Integer length = (Integer) params.get("length");

        String sql = "select o.*,d.dname from doctor o inner join department d on o.did=d.did ";

        sql += createCondition(params);

        sql += " order by o.id limit ?,? ";

        System.out.println(sql);

        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet resultSet = jdbcUtil.executeQuery(sql, start, length);
        try {
            while (resultSet.next()) {
                Doctor doctor = new Doctor();

                Date time = new Date(resultSet.getDate("birthday").getTime());
                Integer age = getAge(time);

                doctor.setId(resultSet.getInt("id"));
                doctor.setName(resultSet.getString("name"));
                doctor.setSex(resultSet.getString("sex"));
                doctor.setBirthday(resultSet.getDate("birthday"));
                doctor.setDid(resultSet.getInt("did"));
                doctor.setDname(resultSet.getString("dname"));
                doctor.setAge(age);

                doctors.add(doctor);
            }
            return doctors;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        } finally {
            jdbcUtil.close();
        }
    }
    private Integer getAge(Date birthday){

        Calendar calendar = Calendar.getInstance();
        int yearNow = calendar.get(Calendar.YEAR);  //当前年份
        int monthNow = calendar.get(Calendar.MONTH);  //当前月份
        int dayOfMonthNow = calendar.get(Calendar.DAY_OF_MONTH); //当前日期
        calendar.setTime(birthday);
        int yearBirth = calendar.get(Calendar.YEAR);
        int monthBirth = calendar.get(Calendar.MONTH);
        int dayOfMonthBirth = calendar.get(Calendar.DAY_OF_MONTH);
        int age = yearNow - yearBirth;   //计算整岁数
        if (monthNow <= monthBirth) {
            if (monthNow==monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                }
            }else {
                age--;
            }
        }
        return age;
    }

    @Override
    public Integer selectDoctorsCount(Map<String, Object> params) {
        String sql = "select count(*) from doctor o ";
        sql += createCondition(params);
        System.out.println(sql);
        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet resultSet = jdbcUtil.executeQuery(sql);

        try {
            if (resultSet.next()){
                return resultSet.getInt(1);
            }else{
                return null;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }finally {
            jdbcUtil.close();
        }
    }
    private String createCondition(Map<String,Object>params){
        StringBuffer buffer = new StringBuffer();
        Integer pub = (Integer) params.get("pub");
        if (pub != null && pub != 0) {
            if (buffer.length() > 0) {
                buffer.append("and ");
            }
            buffer.append("and o.did = " + pub + " ");
        }

        if (buffer.length()>0){
            return " where 1 "+buffer.toString();
        }else{
            return " ";
        }
    }

    @Override
    public Integer insertOneDoctor(Doctor doctor) {
        JDBCUtil jdbcUtil =new JDBCUtil();
        String sql ="insert into doctor(name,birthday,sex,did) values(?,?,?,?) ";

        Integer status = jdbcUtil.executeUpdate(sql,true,doctor.getName(),doctor.getBirthday(),doctor.getSex(),doctor.getDid());
        jdbcUtil.close();
        return status;
    }

    @Override
    public Integer updateOneDoctor(Doctor doctor) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        String sql = "update doctor set name=?, birthday=?, sex=?, did=? where id=? ";
        Integer status = jdbcUtil.executeUpdate(sql,false,doctor.getName(),doctor.getBirthday(),doctor.getSex(),doctor.getDid(),doctor.getId());
        jdbcUtil.close();
        return status;
    }
        @Override
    public Doctor selectDoctorById(Integer id) {
        List<Doctor> doctors = new ArrayList<>();
        String sql ="select o.*,d.dname from doctor o inner join department d on o.did=d.did where o.id=? ";
        System.out.println(sql);
        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet resultSet = jdbcUtil.executeQuery(sql,id);
        try {
            while (resultSet.next()) {
                Doctor doctor = new Doctor();
                Date time = new Date(resultSet.getDate("birthday").getTime());
                Integer age = getAge(time);

                doctor.setId(resultSet.getInt("id"));
                doctor.setName(resultSet.getString("name"));
                doctor.setSex(resultSet.getString("sex"));
                doctor.setBirthday(resultSet.getDate("birthday"));
                doctor.setDid(resultSet.getInt("did"));
                doctor.setDname(resultSet.getString("dname"));
                doctor.setAge(age);
                doctors.add(doctor);

            }
            return doctors.size()>0?doctors.get(0):null;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }finally {
            jdbcUtil.close();
        }
    }

    @Override
    public Integer deleteAllDoctors(Integer id) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        String sql = "delete from doctor where id=? ";
        Integer status = jdbcUtil.executeUpdate(sql,false,id);
        jdbcUtil.close();
        return status;
    }
}
